package com.uphone.magicleafteacher.pro.activity;

import android.view.View;

import com.radish.baselibrary.Intent.IntentUtils;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseListActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.pro.activity.courselivecreate.CourseLiveCreateActivity;
import com.uphone.magicleafteacher.pro.adapter.CoureAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.CourseLiveListEvent;
import com.uphone.magicleafteacher.vo.TeacherLiveBean;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.ResponseBody;


public class CourseLiveListActivity extends BaseListActivity<TeacherLiveBean.ClassListBean, CoureAdapter> {


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(CourseLiveListEvent outGroupEvent) {
        //刷新
        initData();
    }

    @Override
    protected void initLayoutBefore() {
        super.initLayoutBefore();
        isEventBus = true;
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected void initTitle() {
        initTitle("直播课程", R.mipmap.ic_create, view -> {
            CommonUtil.startIntent(CourseLiveListActivity.this, CourseLiveCreateActivity.class);
        });

    }

    @Override
    protected Observable<ResponseBody> loadData(ApiService apiService) {
        return MyApp.apiService(ApiService.class)
                .teacherLive(CommonUtil.getTeacherId(), page + "", "10");
    }

    @Override
    protected List<TeacherLiveBean.ClassListBean> getDataList(String json) {
        TeacherLiveBean bean = GsonUtils.getGson().fromJson(json, TeacherLiveBean.class);
        if (bean != null)
            return bean.getClassList();
        return null;
    }

    @Override
    protected void itemChildClick(CoureAdapter adapter, View view, int position) {
        super.itemChildClick(adapter, view, position);
        TeacherLiveBean.ClassListBean item = adapter.getItem(position);
        if (item.getLiveStatus() == 0 || item.getLiveStatus() == 1) {
            CommonUtil.startIntent(this, CourseLiveWorkActivity.class, item.getLiveId());
        } else {
            IntentUtils.getInstance().with(this, CourseLiveStudentListActivity.class)
                    .putInt("id", item.getLiveId())
                    .putInt("liveStatus", item.getLiveStatus())
                    .start();
        }
    }

    @Override
    protected void itemClick(CoureAdapter adapter, View view, int position) {
        TeacherLiveBean.ClassListBean item = adapter.getItem(position);
        if (item.getLiveStatus() == 2 || item.getLiveStatus() == 3) {
            IntentUtils.getInstance().with(this, CourseLiveStudentListActivity.class)
                    .putInt("id", item.getLiveId())
                    .putInt("liveStatus", item.getLiveStatus())
                    .start();
        } else if (item.getLiveStatus() == 0) {//未开始可编辑
            IntentUtils.getInstance().with(this, CourseLiveCreateActivity.class)
                    .putInt("liveId", item.getLiveId())
                    .start();
        }
    }

    @Override
    protected CoureAdapter initAdapter() {
        return new CoureAdapter();
    }
}
